package com.zj.services;

import com.zj.pojo.Apple;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

import static com.zj.pojo.MyDataPool.apppool;

@Service
public class AppleService implements BaseService {


    public int insertApples(Apple apple) {
        boolean add = apppool.add(apple);
        if (apple.getId() == null) {
            apple.setId(apppool.size());
        }
        return apppool.size();
    }

    public Apple getAppleById(int id) {
        return apppool.get(id);
    }

    public List<Apple> updateAppleById(int id, Apple apple) {
        apple.setId(id);
//        apppool.add(id-1,apple);
//        apppool.remove(id);
        apppool.set(id - 1, apple);
        return Collections.unmodifiableList(apppool);
    }

    public List<Apple> getApppool() {
        return Collections.unmodifiableList(apppool);
    }

    public List<Apple> deleteApple(int id) {
        apppool.remove(id - 1);
        return Collections.unmodifiableList(apppool);
    }
}
